package com.useeinfo.oa.modules.base.biz;

import com.useeinfo.framework.extend.biz.CrudBiz;
import com.useeinfo.framework.sugar.data.QueryParam;
import com.useeinfo.framework.sugar.tools.DateTimeUtils;
import com.useeinfo.oa.modules.base.dao.FacilityDataDao;
import com.useeinfo.oa.modules.base.entity.FacilityData;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @auther: bin.liu
 * @Date: 2018/12/13 10:02
 * @Description: 设备资料service
 */
@Service
public class FacilityDataBiz extends CrudBiz<FacilityDataDao, FacilityData> {


    public boolean existFacilityData(FacilityData facilityData, Long enterpriseId) {
        if (StringUtils.isBlank(facilityData.getFacilityDataName())) {
            return true;
        }

        QueryParam queryParam1 = new QueryParam();
        queryParam1.getSqlMap().put("enterpriseId", enterpriseId+"");
        queryParam1.getSqlMap().put("facilityDataName", facilityData.getFacilityDataName());
        List<FacilityData> facilityDataList = findList(queryParam1);

        if (facilityData.getId() == null) {
            return CollectionUtils.isNotEmpty(facilityDataList);
        } else {
            return !((facilityDataList.size() == 1 && facilityDataList.get(0).getId().equals(facilityData.getId())) || facilityDataList.size() == 0);
        }
    }

    /**
     * 获取流水号
     *
     * @param enterpriseId
     * @return
     */
    public String getSerialNum(String enterpriseId) {
        String serialNum = enterpriseId + "-" + DateTimeUtils.formatDateToStringWithOnlyDate(new Date(), "yyyyMMdd") + "-" + (int) ((Math.random() * 9 + 1) * 100000);
        QueryParam queryParam = new QueryParam();
        queryParam.getSqlMap().put("serialNum", serialNum);
        List<FacilityData> facilityDataList = this.findList(queryParam);
        if (CollectionUtils.isNotEmpty(facilityDataList)) {
            return getSerialNum(enterpriseId);
        } else {
            return serialNum;
        }
    }
}
